//获取信息
async function getScore () {
  const res = await axios({ url: 'score/list' })
  const data = res.data.data
  document.querySelector('tbody').innerHTML = Object.keys(data).map(i => `<tr data-id=${i}>
          <th scope="row" >${i}</th>
          <td>${data[i].name}</td>
          ${data[i].score.map((e, index) => `<td class="score" data-code=${index + 1}>${e}</td>
        `).join('')}
          </tr>`).join('')

}
getScore()

document.querySelector('.table tbody').addEventListener('click', e => {
  const code = e.target.dataset.code
  const id = e.target.parentNode.dataset.id
  if (e.target.classList.contains('score')) {
    const v = e.target.innerHTML
    const input = document.createElement('input')
    input.className = 'inp'
    input.value = v
    e.target.appendChild(input)
    input.focus()
    input.select()
    input.addEventListener('keydown', async e => {
      if (e.key === 'Enter') {
        e.target.innerHTML = input.value
        input.remove()
        const data = {
          stu_id: id,
          batch: code,
          score: input.value
        }
        const res = await axios({ url: 'score/entry', method: 'post', data })
        Toast.success(res.data.message)
        getScore()

      }
    })
  }
})

